package com.itheima.service;

import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class Demo {

    private StudentMapper mapper;
    Student student = new Student();

    @Before
    public void init (){
        //获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        mapper = sqlSession.getMapper(StudentMapper.class);

    }

    //查看全部
    @Test
    public void findAll(){
        List<Student> list = mapper.findAll();
        for (Student student : list) {
            System.out.println(student);
        }
    }

    //id查询
    @Test
    public void findById(){
        Student student = mapper.findById(4);
        System.out.println(student);
    }

    //多条件查询
    @Test
    public void findByName(){
        List<Student> list = mapper.findByName("张三",24);
        for (Student student : list) {
            System.out.println(student);
        }
    }

    //模糊查询
    @Test
    public void findByName2(){
        List<Student> list = mapper.findByName2("三");
        for (Student student : list) {
            System.out.println(student);
        }
    }

    //添加
    @Test
    public void save(){
        student.setName("张三");
        student.setAge(23);
        mapper.save(student);
    }

    //修改
    @Test
    public void update(){
        student.setId(22);
        student.setName("钱七");
        student.setAge(27);
        mapper.update(student);
    }

    //删除
    @Test
    public void delete(){
        mapper.delete(22);
    }
}
